<?php
namespace App\Models;

use Illuminate\Support\Facades\DB;

class ClientUserRelationship extends CommonModel {
	protected $table = 'client_user_relationship';
	
	/**
	 * @param int $parent_id
	 * @return mixed
	 * 递归查询子树
	 */
	public static function treeChildren($parent_id = 0)
	{
		$data = static::select(['u.*', 'client_user_relationship.id', 'client_user_roles.uid', 'client_user_relationship.parent_id'])
              ->leftJoin('client_user_roles as r','r.ship_id','=','client_user_relationship.id')
              ->leftJoin('client_user as u', 'u.id', '=', 'r.uid')
              ->where(['parent_id'=>$parent_id])->get()->each(function ($v, $k) {
			$v->children = static::treeChildren($v->uid);
		});
		return $data;
	}

	public static function getTree($ship,$user){

          $level = $ship->type;
          switch ($level){
               case 1:
                    $data = self::getTreeChildren($ship->uid,0);
                    break;
               case 2:
                    $data=array();
                    $data['0']->uid=$user->id;
                    $data['0']->realname=$user->realname;
                    $data['0']->children = self::getTreeChildren($ship->uid,2);
                    break;    
               case 3:
                    $data['0'] = DB::table('client_user_relationship')->select(['u.id','u.realname'])->leftJoin('client_user as u', 'u.id', '=', 'client_user_relationship.parent_id')->where(['client_user_relationship.uid'=>$ship->parent_id])->first();
                    $data['0']->children['0']->uid = $user->id;
                    $data['0']->children['0']->realname = $user->realname;
                    $data['0']->children['0']->children = self::getTreeChildren($ship->uid,3);
                    break;
               case 4:
                    $data = DB::select("SELECT u.id AS uid, u.realname FROM client_user_relationship b RIGHT JOIN ( SELECT parent_id FROM client_user_relationship WHERE uid = ".$ship->uid.") a ON b.uid = a.parent_id LEFT JOIN client_user u ON u.id = b.parent_id");

                    $data['0']->children['0']=
                        DB::table('client_user_relationship')
                            ->Leftjoin('client_user','client_user.id','=','client_user_relationship.parent_id')
                            ->where(['client_user_relationship.uid'=>$ship->uid])
                            ->first(['client_user.id','client_user.realname','client_user_relationship.id']);
                    $data['0']->children['0']->children['0']->uid = $user->uid;
                    $data['0']->children['0']->children['0']->realname = $user->realname;

                    break;
               default:
                    return array();
          }
          return $data;
     }

     /**
      * @param int $parent_id
      * @return mixed
      * 递归查询子树
      */
     public static function getTreeChildren($parent_id = 0,$type=0)
     {
          if($type==0){
               $data=collect([]);
               static::select(['u.realname', 'roles.uid'])
                   ->RightJoin('client_user_roles as roles', 'client_user_relationship.id', '=', 'roles.ship_id')
                   ->leftJoin('client_user as u', 'u.id', '=', 'roles.uid')
                   ->where(['parent_id' => $parent_id])
                   ->get()->each(function ($v, $k) use ($type,&$data) {
                        $data = static::getTreeChildren($v->uid, $type + 1);
                   });
          }else if($type!=3) {
               $data = static::select(['u.realname', 'client_user_relationship.uid'])
                   ->leftJoin('client_user as u', 'u.id', '=', 'client_user_relationship.uid')
                   ->where(['parent_id' => $parent_id])
                   ->get()
                   ->each(function ($v, $k) use ($type) {
                        $v->children = static::getTreeChildren($v->uid, $type + 1);
                   });
          }else{
               $data = static::select(['u.realname', 'roles.uid'])
                   ->RightJoin('client_user_roles as roles', 'client_user_relationship.id', '=', 'roles.ship_id')
                   ->leftJoin('client_user as u', 'u.id', '=', 'roles.uid')
                   ->where(['parent_id' => $parent_id])
                   ->get()
                   ->each(function ($v, $k) use ($type) {
                        $v->children = array();
                   });

          }
          return $data;
     }
	/**
	 * @param int $parent_id
	 * @return \Illuminate\Support\Collection
	 * 递归查询子树并返回一维数据
	 */
	public static function treeChildrenOne($parent_id = 0,$type=1){
		$return = collect([]);
		if($type==1){
		     $to_type=2;
          }else if($type==2){
		     $to_type=5;
          }else if($type ==5){
               $to_type=3;
          }else if($type==4){
               return $return;
          }

		if($type!=3 ) {
               static::select(['uid', 'realname'])->leftJoin('client_user as u', 'u.id', '=', 'client_user_relationship.uid')
                   ->where(['parent_id' => $parent_id])->get()->each(function ($v, $k) use (&$return, $to_type) {
                        $parent = static::treeChildrenOne($v->uid, $to_type);
                        $return->push($v);
                        $return = $return->merge($parent);
                   });
          } else if($type == 3 ){
               static::select(['u.realname','roles.uid'])
                   ->rightJoin('client_user_roles as roles', 'roles.ship_id', '=', 'client_user_relationship.id')
                   ->leftJoin('client_user as u', 'u.id', '=', 'roles.uid')
                   ->where(['client_user_relationship.parent_id' => $parent_id])->get()->each(function ($v, $k) use (&$return) {
                        $return->push($v);
                   });

          }
		return $return;
	}
	
	public static function getLevel($uid){
	    $level = static::select('type')->where('uid',$uid)->first();
	    return $level->type;
	}
}